/* @(#)GSOperatorScanQueryAction.java    
*
* Project: BUS
*
* Modify Information:
* =============================================================================
*   Author       Date       Description
*   ------------ ---------- ---------------------------------------------------
*   Simonyi Peng 2006-2-12    建立
*
* Copyright Notice:
* =============================================================================
*    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
*        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
*        Haidian District, Beijing ,100081, China All rights reserved.
*
*    This software is the confidential and proprietary information of
*        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
*        You shall not disclose such Confidential Information and shall use it
*        only in accordance with the terms of the license agreement you entered
*        into with HiTRUST.
*
* Warning:
* =============================================================================
* 
*/
package com.hitrust.gs.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderManufactureVO;
import com.hitrust.gs.vo.GSOrderTempManufactureVO;
import com.hitrust.gs.vo.GSOrderTempVO;
import com.hitrust.gs.vo.GSOrderVO;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSOrderBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSTempOrderProcesser;
import com.hitrust.gs.form.GSOrderSaveForm;
import com.hitrust.gs.form.GSOrderTempSaveForm;

import java.lang.reflect.Method;

/**
 * @author simonyi
 *
 * 订单保存
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSOrderTempSaveAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
	    
        GSOperatorBean bean = (GSOperatorBean) aForm.getSession().getAttribute(
                GSBaseConstants.LOGIN);

	    GSOrderTempSaveForm tForm = (GSOrderTempSaveForm)aForm;
	    
	    GSOrderTempVO orderTempvo = new GSOrderTempVO();
	    GSManufactureProcessor proc = new GSManufactureProcessor();
	    
	    
	    //ordervo.orderid = (String)request.getParameter("orderid");
	    orderTempvo.gs_balance_mode = "2".equals(tForm.getBalancemode())?tForm.getBalancemode_():tForm.getBalancemode();
	    orderTempvo.gs_deliver_mode = "2".equals(tForm.getDelivermode())?tForm.getDelivermode_():tForm.getDelivermode();
	    orderTempvo.gs_deliver_goods_addr = tForm.getDeliveraddr();
	    orderTempvo.gs_workcorp_id = tForm.getDeptid();
	    orderTempvo.gs_deliveryman = tForm.getLinkman_()==null?"":tForm.getLinkman_();
	    orderTempvo.gs_deliver_goods_phone = tForm.getLinktel_()==null?"":tForm.getLinktel_();
	    orderTempvo.gs_notes = tForm.getNotes();
	    orderTempvo.gs_signin_date = tForm.getSignindate();
	    orderTempvo.gs_signin_person = tForm.getSigninperson();
	    orderTempvo.gs_deliver_goods_id = tForm.getDelivergoodsid();
	    
	    orderTempvo.operId = bean.getGs_oper_id();
	    orderTempvo.orgId = bean.getGs_oper_org_id();
	    orderTempvo.balanceCategory = tForm.getBalancecategory();
	    
	    ArrayList ary = new ArrayList();
	    
	    String[] concentricityId = tForm.getConcentricityid_();
	    String[] manufactureId = tForm.getManufactureid();
	    String[] orderCount = tForm.getOrdercount();
	    String[] referencePrice = tForm.getReferenceprice();
	    String[] signinPrice = tForm.getSigninprice();
	    String[] mode=tForm.getMode();
	    String providerId = tForm.getProviderid();
	    try{
		    for(int i=0;i<10;i++){
		        GSOrderTempManufactureVO vo = new GSOrderTempManufactureVO();
		        
		        if(concentricityId[i] == null || concentricityId[i].length() == 0)
		            continue;
		        vo.concentricityId = concentricityId[i];
		        vo.orderCount = orderCount[i];
		        vo.referencePrice = Double.parseDouble("".equals(referencePrice[i])?"0":referencePrice[i]);
		        vo.signinPrice = Double.parseDouble("".equals(signinPrice[i])?"0":signinPrice[i]);
		        vo.providerId = providerId;
		        //if(i==0){
		            vo.manufactureId = proc.getManufactureIdByConcentricityIdAndProviderId(vo.concentricityId,providerId);
		        //}else{
		        //    vo.manufactureId = manufactureId[i];    
		        //}
		        vo.balanceCategory = orderTempvo.balanceCategory;
		        vo.mode=mode[i];
		        ary.add(vo);
		    }
	        
	    }catch(Exception e){}
	    
	    GSTempOrderProcesser processer = new GSTempOrderProcesser();
	    try{
	        String orderId = processer.saveOrder(bean,orderTempvo,ary);//"010000713L";
	        
	        String saveOption = aForm.getRequest().getParameter("saveoption");
	        if("SA".equals(saveOption)){
	            aForm.getRequest().setAttribute("SUCCESSMSG","应急采购订单保存成功，编号为 "+orderId);
	            return "new";//允许录入新申购单
	        }
	        if("S".equals(saveOption)){
		        ArrayList arylist = processer.getOrderDetail_(orderId);
		        
		        aForm.getRequest().setAttribute("arylist",arylist);
		        ArrayList bcList = GSBaseManager.getAllBalanceCategory();
		        aForm.getRequest().setAttribute("balancecategory",bcList);
		        aForm.getRequest().setAttribute("SUCCESSMSG","应急采购订单保存成功，编号为 "+orderId);
		        return GSBaseConstants.FORWARD_SUCCESS;
	        }
	        return GSBaseConstants.FORWARD_FAILURE;
	    }
	    catch(GSException e){
			GSLogWriter.error(this, e.getMessage());
			aForm.getRequest().setAttribute("action", "A110101.do");
			throw e;	        
	    }
	    
	}
}
